﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP.BaseInfo
{
    //[Module("FrmLogisticsEdit")]
    public partial class FrmLogisticsEdit : ModuleForm
    {
        Clientlogisticsinfo _logisticsinfo;
        readonly LogisticsinfoBLL _logisticsBll;
        public FrmLogisticsEdit()
        {
            InitializeComponent();

            _logisticsBll = new LogisticsinfoBLL();
            _logisticsinfo = new Clientlogisticsinfo();
            BindFormContro();
        }

        public FrmLogisticsEdit(Clientlogisticsinfo obj)
        {
            InitializeComponent();

            _logisticsBll = new LogisticsinfoBLL();
            _logisticsinfo = obj;

            BindFormContro();
        }

        /// <summary>
        /// 绑定控件值
        /// </summary>
        void BindFormContro()
        {
            txtClientname.DataBindings.Add(new Binding("Text", _logisticsinfo, "Clientname", true, DataSourceUpdateMode.OnPropertyChanged));
            txtClientpym.DataBindings.Add(new Binding("Text", _logisticsinfo, "Clientpym", true, DataSourceUpdateMode.OnPropertyChanged));
            txtClientshortname.DataBindings.Add(new Binding("Text", _logisticsinfo, "Clientshortname", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAddress.DataBindings.Add(new Binding("Text", _logisticsinfo, "Address", true, DataSourceUpdateMode.OnPropertyChanged));
            txtLinkman.DataBindings.Add(new Binding("Text", _logisticsinfo, "Linkman", true, DataSourceUpdateMode.OnPropertyChanged));
            txtLinkTel.DataBindings.Add(new Binding("Text", _logisticsinfo, "Linktel", true, DataSourceUpdateMode.OnPropertyChanged));
            txtMobilephone.DataBindings.Add(new Binding("Text", _logisticsinfo, "Mobilephone", true, DataSourceUpdateMode.OnPropertyChanged));
            txtFax.DataBindings.Add(new Binding("Text", _logisticsinfo, "Fax", true, DataSourceUpdateMode.OnPropertyChanged));
            txtEmail.DataBindings.Add(new Binding("Tag", _logisticsinfo, "Email", true, DataSourceUpdateMode.OnPropertyChanged));
            txtPostcode.DataBindings.Add(new Binding("Text", _logisticsinfo, "Postcode", true, DataSourceUpdateMode.OnPropertyChanged));
            txtBank.DataBindings.Add(new Binding("Text", _logisticsinfo, "Bank", true, DataSourceUpdateMode.OnPropertyChanged));
            txtAccountno.DataBindings.Add(new Binding("Text", _logisticsinfo, "Accountno", true, DataSourceUpdateMode.OnPropertyChanged));
            txtClienturl.DataBindings.Add(new Binding("Text", _logisticsinfo, "Clienturl", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNote.DataBindings.Add(new Binding("Text", _logisticsinfo, "Note", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield1.DataBindings.Add(new Binding("Text", _logisticsinfo, "Exfield1", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield2.DataBindings.Add(new Binding("Text", _logisticsinfo, "Exfield2", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield3.DataBindings.Add(new Binding("Text", _logisticsinfo, "Exfield3", true, DataSourceUpdateMode.OnPropertyChanged));
            txtExfield4.DataBindings.Add(new Binding("Text", _logisticsinfo, "Exfield4", true, DataSourceUpdateMode.OnPropertyChanged));
            diRecmoney.DataBindings.Add(new Binding("ValueObject", _logisticsinfo, "Recmoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diPaymoney.DataBindings.Add(new Binding("ValueObject", _logisticsinfo, "Paymoney", true, DataSourceUpdateMode.OnPropertyChanged));
            diRecmoneysum.DataBindings.Add(new Binding("ValueObject", _logisticsinfo, "Recmoneysum", true, DataSourceUpdateMode.OnPropertyChanged));
            diPaymoneysum.DataBindings.Add(new Binding("ValueObject", _logisticsinfo, "Paymoneysum", true, DataSourceUpdateMode.OnPropertyChanged));
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            _logisticsinfo = new Clientlogisticsinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
            txtClientname.Focus();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }

        private void SaveInfo(bool isAdd = true)
        {
            if (txtClientname.Text.Length == 0)
            {
                MessageBox.Show("公司名称不能为空，请输入.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtClientname.Focus();
                return;
            }

            _logisticsBll.Edit(_logisticsinfo);

            MessageBox.Show("保存成功.", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (isAdd) this.DialogResult = DialogResult.OK;
        }

        private void tsbSaveAdd_Click(object sender, EventArgs e)
        {
            SaveInfo(false);
            _logisticsinfo = new Clientlogisticsinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsmPre_Click(object sender, EventArgs e)
        {
            PreviewRecord(_logisticsinfo.Id - 1, 0);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            PreviewRecord(_logisticsinfo.Id + 1, 1);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        private void PreviewRecord(long id, int type)
        {
            bool isLast = false;
            var obj = _logisticsBll.PreRecord(id, type, ref isLast);
            _logisticsinfo = obj;
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();

            if (isLast) MessageBox.Show(string.Format("当前已是{0}一条记录", type.Equals(0) ? "第" : "最后"));
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUsername_Leave(object sender, EventArgs e)
        {
            txtClientpym.Text = ChineseToSpell.GetChineseSpell(txtClientname.Text);
        }
    }
}
